/**
 A jQuery plugin for search hints

 Author: Lorenzo Cioni - https://github.com/lorecioni
 */

(function ($) {
    $.fn.autocomplete = function (params) {

        //Selections
        var currentSelection = -1;
        var currentProposals = [];

        //Default parameters 设置默认参数
        params = $.extend({
            hints: [],
             placeholder: '请输入受检者的民族',
            width: 160,
            height:40,
            showButton: false,
            mustMatch : false,
            matchContains : true,
            /*buttonText: '搜索',*/
            onSubmit: function (text) { },
            onBlur: function () { }
        }, params);

        //Build messagess
        this.each(function () {
            //Container 搜索框的外包装样式，其中height是页面设置的height，height:15
            var searchContainer = $('<div></div>')
                .addClass('autocomplete-container')
                .css('height', params.height );

            //Text input  搜索文本框样式
            var input = $('<input type="text" autocomplete="off" name="query" id="getNationaltily">')
                .attr('placeholder', params.placeholder)
                .addClass('autocomplete-input')
                .css({
                    'width': params.width,
                    'height': params.height
                });

            /* if (params.showButton) {
                 input.css('border-radius', '3px 0 0 3px');
             }*/

            //Proposals
            var proposals = $('<div></div>')
                .addClass('proposal-box')
                .css('width', params.width )
                .css('top', input.height() + 10);
            var proposalList = $('<ul></ul>')
                .addClass('proposal-list');

            proposals.append(proposalList);

            input.keydown(function (e) {
                switch (e.which) {
                    case 38: // Up arrow
                        e.preventDefault();
                        $('ul.proposal-list li').removeClass('selected');
                        if ((currentSelection - 1) >= 0) {
                            currentSelection--;
                            $("ul.proposal-list li:eq(" + currentSelection + ")")
                                .addClass('selected');
                        } else {
                            currentSelection = -1;
                        }
                        break;
                    case 40: // Down arrow
                        e.preventDefault();
                        if ((currentSelection + 1) < currentProposals.length) {
                            $('ul.proposal-list li').removeClass('selected');
                            currentSelection++;
                            $("ul.proposal-list li:eq(" + currentSelection + ")")
                                .addClass('selected');
                        }
                        break;
                    case 13: // Enter
                        if (currentSelection > -1) {
                            var text = $("ul.proposal-list li:eq(" + currentSelection + ")").html();
                            input.val(text);
                            //将选择的值放入隐藏文本框
                            $("#nationaltily").val($("#getNationaltily").val());
                        }
                        currentSelection = -1;
                        proposalList.empty();
                        params.onSubmit(input.val());
                        break;
                    case 27: // Esc button
                        currentSelection = -1;
                        proposalList.empty();
                        input.val('');
                        $("#nationaltily").val(text);
                        break;
                }
            });

            //搜索框绑定方法
            input.bind("change paste keyup", function (e) {
                if (e.which != 13 && e.which != 27
                    && e.which != 38 && e.which != 40) {
                    currentProposals = [];
                    currentSelection = -1;
                    proposalList.empty();
                    if (input.val() != '') {
                        var word = "^" + input.val() + ".*";
                        proposalList.empty();
                        for (var test in params.hints) {
                            if (params.hints[test].match(word)) {
                                currentProposals.push(params.hints[test]);
                                var element = $('<li></li>').html(params.hints[test]).addClass('proposal').click(function () {
                                    input.val($(this).html());
                                    //将选择的值放入隐藏文本框
                                    $("#nationaltily").val($("#getNationaltily").val());
                                    proposalList.empty();
                                    params.onSubmit(input.val());
                                })
                                    .mouseenter(function () {
                                        $(this).addClass('selected');
                                    })
                                    .mouseleave(function () {
                                        $(this).removeClass('selected');
                                    });
                                proposalList.append(element);
                            }
                        }
                    }
                }
            });

            input.blur(function (e) {
                currentSelection = -1;
                //proposalList.empty();
                params.onBlur();
            });

            searchContainer.append(input);
            searchContainer.append(proposals);



            $(this).append(searchContainer);


        });

        return this;
    };

})(jQuery);